package com.hjm.system.service.cuser.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.hjm.common.core.constants.CacheConstants;
import com.hjm.common.core.constants.Constants;
import com.hjm.common.core.enums.ResultCode;
import com.hjm.redis.service.RedisService;
import com.hjm.security.exception.ServiceException;
import com.hjm.system.domain.cuser.dto.UserDTO;
import com.hjm.system.domain.cuser.dto.UserQueryDTO;
import com.hjm.system.domain.cuser.vo.UserVO;
import com.hjm.system.mapper.cuser.UserMapper;
import com.hjm.system.service.cuser.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.system.domain.cuser.User;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RedisService redisService;

    @Override
    public List<UserVO> list(UserQueryDTO userQueryDTO) {

        PageHelper.startPage(userQueryDTO.getPageNum(), userQueryDTO.getPageSize());

        return userMapper.selectUserList(userQueryDTO);
    }

    @Override
    public int block(UserDTO userDTO) {
        User user = userMapper.selectById(userDTO.getUserId());
        if(BeanUtil.isEmpty(user)){
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        user.setStatus(userDTO.getStatus());
        redisService.deleteCacheMapValue( CacheConstants.USER_KEY,user.getUserId().toString());
        redisService.setCacheMapValue(CacheConstants.USER_KEY,user.getUserId().toString(),user);
        return userMapper.updateById(user);
    }
}
